﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Data;

namespace newTurtle8
{
    class Translator
    {
        private static Dictionary<string, string> dict = new Dictionary<string, string>();
        private static SqlCommand selectCmd = SqlManager.SqlCommandFactory(@"SELECT * FROM EnglishHebrewDictionary");

        static Translator()
        {
            dict = new Dictionary<string, string>();
            createEnglishHebrewDictionaryListFromDatabase();
        }

        static private void createEnglishHebrewDictionaryListFromDatabase()
        {
            SqlDataReader reader = SqlManager.getSqlDataReader(selectCmd);
            while (reader.Read())
                dict.Add((string)reader["engWord"], (string)reader["hebWord"]);

            selectCmd.Connection.Close();
            reader.Close();
        }

        /// <summary>
        /// gets a collection of Columns. 
        /// sets it's HeaderText property to it's Hebrew display.
        /// </summary>
        /// <param name="collection"></param>
        static public void setTableColumnHeadersHebrew(DataGridViewColumnCollection collection)
        {
            Regex objNaturalPattern = new Regex("[0-9]");
            foreach (DataGridViewColumn item in collection)
            {
                item.HeaderText = objNaturalPattern.Replace(item.HeaderText, string.Empty);
                item.HeaderText = translateEnglishWord(item.Name);
            }
        }

        static public void setTableColumnHeadersHebrew(DataColumnCollection collection)
        {
            Regex objNaturalPattern = new Regex("[0-9]");
            foreach (DataColumn item in collection)
            {
                item.ColumnName = objNaturalPattern.Replace(item.ColumnName, string.Empty);
                item.ColumnName = translateEnglishWord(item.ColumnName);
            }
        }

        /// <summary>
        /// receives an english word. 
        /// </summary>
        /// <param name="engWord"></param>
        /// <returns>the hebrew string as appears in the data base.</returns>
        static public string translateEnglishWord(string engWord)
        {
            if (engWord == string.Empty)
                return string.Empty;
            return dict[engWord];
        }

        /// <summary>
        /// receives an hebrew word.  
        /// </summary>
        /// <param name="hebWord"></param>
        /// <returns>the english string as appears in the data base.</returns>
        static public string translateHebrewWord(string hebWord)
        {
            Dictionary<string, string>.KeyCollection keys = dict.Keys;

            foreach (string key in keys)
                if (dict[key].Equals(hebWord))
                    return key;
            return string.Empty;
        }
    }
}
